package com.sgb.kjwl.view.ui.activity.self.publish;

import android.content.Context;
import android.content.Intent;

import androidx.databinding.DataBindingUtil;

import android.os.Bundle;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.text.TextUtils;
import android.view.View;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityMachinePublishTypeBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.link.entity.PublicTypeEntity;
import com.sgb.link.entity.ReturnTypeEntity;
import com.sgb.kjwl.view.event.self.MachinePublishEvent;
import com.sgb.kjwl.view.ui.adapter.self.publish.MachinePublishTypeLAdapter;
import com.sgb.kjwl.view.ui.adapter.self.publish.MachinePublishTypeRAdapter;
import com.sgb.kjwl.viewmodel.self.publish.MachinePublishTypeModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import javax.inject.Inject;

import static com.swgk.core.event.EventConstant.EVENT_PUBLISH_DEVICE_RETURN;
import static com.swgk.core.event.EventConstant.EVENT_PUBLISH_MACHINE_RETURN;

public class PublicPublishTypeActivity extends AppActivity implements ItemInputTextView.OnWholeItemClickListener {
    private ActivityMachinePublishTypeBinding binding;
    private MachinePublishTypeLAdapter machinePublishTypeLAdapter;
    private MachinePublishTypeRAdapter machinePublishTypeRAdapter;
    private List<PublicTypeEntity<List<PublicTypeEntity>>> publicTypeEntityList;
    private List<PublicTypeEntity<List<PublicTypeEntity>>> publicTypeEntityChildList;
    private String firstName;
    private String secondName;
    private String firstNo;
    private String secondNo;
    private ReturnTypeEntity returnTypeEntity;
    private int type;
    @Inject
    MachinePublishTypeModel viewModel;

    public static void start(Context context) {
        start(context, null);
    }

    public static void start(Context context, Intent extras) {
        Intent intent = new Intent();
        intent.setClass(context, PublicPublishTypeActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        if (extras != null) {
            intent.putExtras(extras);
        }
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder()
                .appComponent(appComponent)
                .selfModule(new SelfModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(PublicPublishTypeActivity.this, R.layout.activity_machine_publish_type);
        binding.topLayout.ivBack.setOnClickListener(v -> finish());
        binding.topLayout.titleView.setText("类型选择");
    }

    @Override
    protected void init() {
        returnTypeEntity = new ReturnTypeEntity();
        int type = getIntent().getIntExtra("type", 0);
        publicTypeEntityList = (List<PublicTypeEntity<List<PublicTypeEntity>>>) getIntent().getSerializableExtra("publicTypeEntityList");
        machinePublishTypeLAdapter = new MachinePublishTypeLAdapter(PublicPublishTypeActivity.this, null);
        machinePublishTypeLAdapter.setItemSelect(true);
        machinePublishTypeLAdapter.setDefItem(0);
        binding.leftRecycler.setLayoutManager(new LinearLayoutManager(PublicPublishTypeActivity.this));
        binding.leftRecycler.setAdapter(machinePublishTypeLAdapter);
        machinePublishTypeLAdapter.setOnItemClick(new AdapterOnItemClick<PublicTypeEntity<List<PublicTypeEntity>>>() {
            @Override
            public void onItemClick(PublicTypeEntity<List<PublicTypeEntity>> publicTypeEntity, int position) {
                firstName = publicTypeEntity.getName();
                firstNo = publicTypeEntity.getNo();
//                viewModel.getMachineTypeModel();
                machinePublishTypeRAdapter.setDatas(publicTypeEntity.getChildren());
            }
        });

        machinePublishTypeRAdapter = new MachinePublishTypeRAdapter(PublicPublishTypeActivity.this, null);
        machinePublishTypeRAdapter.setItemSelect(true);
        machinePublishTypeRAdapter.setDefItem(0);
        binding.rightRecycler.setLayoutManager(new GridLayoutManager(PublicPublishTypeActivity.this, 3));
        binding.rightRecycler.setAdapter(machinePublishTypeRAdapter);
        machinePublishTypeRAdapter.setOnItemClick(new AdapterOnItemClick<PublicTypeEntity>() {
            @Override
            public void onItemClick(PublicTypeEntity publicTypeEntity, int position) {
                secondName = publicTypeEntity.getName();
                secondNo = publicTypeEntity.getNo();
                if (TextUtils.isEmpty(firstName)) {
                    returnTypeEntity.setFirstName(publicTypeEntityList.get(0).getName());
                    returnTypeEntity.setFirstNo(publicTypeEntityList.get(0).getNo());
                } else {
                    returnTypeEntity.setFirstName(firstName);
                    returnTypeEntity.setFirstNo(firstNo);
                }
                returnTypeEntity.setSecondName(secondName);
                returnTypeEntity.setSecondNo(secondNo);
                //---发送
                MachinePublishEvent machinePublishEvent = new MachinePublishEvent();
                if (type == EVENT_PUBLISH_DEVICE_RETURN) {
                    machinePublishEvent.setMessageTag(EVENT_PUBLISH_DEVICE_RETURN);
                } else {
                    machinePublishEvent.setMessageTag(EVENT_PUBLISH_MACHINE_RETURN);
                }
                machinePublishEvent.setMessageData(returnTypeEntity);
                EventBus.getDefault().post(machinePublishEvent);
                finish();
            }
        });
        setDate();
    }

    public void getMachineTypeSucc(List<PublicTypeEntity<List<PublicTypeEntity>>> datas) {
        this.publicTypeEntityChildList = datas;
//        machinePublishTypeRAdapter.setDatas(publicTypeEntityChildList);
    }

    public void setDate() {
        machinePublishTypeLAdapter.setDatas(publicTypeEntityList);
        if (publicTypeEntityList != null) {
            machinePublishTypeRAdapter.setDatas(publicTypeEntityList.get(0).getChildren());
            String no = publicTypeEntityList.get(0).getNo();
        }
//        viewModel.getMachineTypeModel();
    }

    @Override
    public void OnWholeItemClick(View view) {
        switch (view.getId()) {
            case R.id.item_tv_type:
                PackersTypeChooicetActivity.start(this);
                break;
        }
    }
}

